using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Windows.Forms;

using FreeFlow.Administration;
using FreeFlow.WinFormsControls.HelperClasses;

namespace FreeFlowAdministrator
{
	/// <summary>
	/// Summary description for FolderListControl.
	/// </summary>
	public class FoldersControl : AdminUserControl
  {
    private FreeFlowAdministrator.ExtendedDataGrid foldersGrid;
    private FreeFlowAdministrator.UIActionsControl uiActionsControl;
    private IContainer components;

		public FoldersControl()
		{
			// This call is required by the Windows.Forms Form Designer.
			InitializeComponent();

      actionList = new UIActionList();
      actionList.Add("Refresh", "Refresh the list of folders", 
        new ActionExecuteDelegate(RefreshFolders));
      actionList.Add("Open selected folders", "Open the selected folders", 
        new ActionExecuteDelegate(OpenSelected));
      actionList.Add("Open in client", "Open the selected folders in the client",
        new ActionExecuteDelegate(OpenSelectedInClient));
      actionList.Add("Delete selected folders", "Delete the selected folders", 
        new ActionExecuteDelegate(DeleteFolders));
      actionList.Add("Copy to clipboard", "Copy the list of folders to the clipboard", 
        new ActionExecuteDelegate(CopyToClipboard));
      uiActionsControl.Actions = actionList;
		}

		/// <summary> 
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Component Designer generated code
		/// <summary> 
		/// Required method for Designer support - do not modify 
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
      this.components = new System.ComponentModel.Container();
      this.foldersGrid = new FreeFlowAdministrator.ExtendedDataGrid();
      this.uiActionsControl = new FreeFlowAdministrator.UIActionsControl();
      ((System.ComponentModel.ISupportInitialize)(this.foldersGrid)).BeginInit();
      this.SuspendLayout();
      // 
      // foldersGrid
      // 
      this.foldersGrid.AllowNavigation = false;
      this.foldersGrid.CaptionVisible = false;
      this.foldersGrid.DataMember = "";
      this.foldersGrid.Dock = System.Windows.Forms.DockStyle.Fill;
      this.foldersGrid.HeaderForeColor = System.Drawing.SystemColors.ControlText;
      this.foldersGrid.Location = new System.Drawing.Point(0, 0);
      this.foldersGrid.Name = "foldersGrid";
      this.foldersGrid.Size = new System.Drawing.Size(498, 384);
      this.foldersGrid.TabIndex = 1;
      this.foldersGrid.OpenSelected += new System.EventHandler(this.foldersGrid_OpenSelected);
      // 
      // uiActionsControl
      // 
      this.uiActionsControl.Actions = null;
      this.uiActionsControl.Dock = System.Windows.Forms.DockStyle.Right;
      this.uiActionsControl.Location = new System.Drawing.Point(498, 0);
      this.uiActionsControl.Name = "uiActionsControl";
      this.uiActionsControl.Size = new System.Drawing.Size(150, 384);
      this.uiActionsControl.TabIndex = 2;
      // 
      // FoldersControl
      // 
      this.Controls.Add(this.foldersGrid);
      this.Controls.Add(this.uiActionsControl);
      this.Name = "FoldersControl";
      this.Size = new System.Drawing.Size(648, 384);
      ((System.ComponentModel.ISupportInitialize)(this.foldersGrid)).EndInit();
      this.ResumeLayout(false);

    }
		#endregion

    private FolderCollection folders;
    public void LoadFolders(FolderCollection folders)
    {
      this.folders = folders;
      foldersGrid.DataSource = null;
      foldersGrid.DataSource = folders;
      foldersGrid.ClearColumns();
      foldersGrid.AddColumn("Name");
      foldersGrid.AddColumn("FolderId", "Folder ID");
      foldersGrid.AddColumn("Subject");
      foldersGrid.AddColumn("Created");
      foldersGrid.AddColumn("Updated", "Last updated");
      foldersGrid.AddColumn("EntryTime", "Entry time");
      foldersGrid.AddColumn("Deadline");
      foldersGrid.AddColumn("Priority");
      foldersGrid.AddColumn("MapName", "Map");
      foldersGrid.AddColumn("StageName", "Stage");
      foldersGrid.AddColumn("Originator");
      foldersGrid.AddColumn("LockedAction", "Locked action");
      foldersGrid.AddColumn("LockedBy", "Locked by");
      foldersGrid.AddColumn("ParentFolderId", "Parent folder ID");
      foldersGrid.AddColumn("Category");
      foldersGrid.AddColumn("IsArchived", "Archived?");
      foldersGrid.AutosizeColumns();
    }

    private string OpenSelected()
    {
      using (new CursorHandler())
      {
        CurrencyManager bm = (CurrencyManager)foldersGrid.BindingContext[foldersGrid.DataSource];
        for (int i = 0; i < bm.Count; i++) 
        { 
          if(foldersGrid.IsSelected(i))
          {
            Folder folder = (Folder)bm.List[i];
            FolderForm form = new FolderForm();
            form.LoadFolder(folder);
            form.Show();
          }
        }
      }
      return null;
    }

    private string OpenSelectedInClient()
    {
      using (new CursorHandler())
      {
        CurrencyManager bm = (CurrencyManager)foldersGrid.BindingContext[foldersGrid.DataSource];
        for (int i = 0; i < bm.Count; i++)
        {
          if (foldersGrid.IsSelected(i))
          {
            Folder folder = (Folder)bm.List[i];
            folder.OpenInClient(OptionsControl.WebDllLocation(folder.Server.Dsn));
          }
        }
      }
      return null;
    }

    private string DeleteFolders()
    {
      if (MessageBox.Show("Delete the selected folders?", "Confirm", MessageBoxButtons.YesNo) == DialogResult.Yes)
      {
        using (new CursorHandler())
        {
          foldersGrid.DeleteSelected();
          OnRefreshRequired();
        }
      }
      return null;
    }

    private string RefreshFolders()
    {
      folders.Refresh();
      foldersGrid.RefreshGrid();
      OnRefreshRequired();
      return null;
    }

    private string CopyToClipboard()
    {
      foldersGrid.CopyToClipboard();
      return null;
    }

    private UIActionList actionList;
    public UIActionList ActionList
    {
      get
      {
        return actionList;
      }
    }

    public bool ActionsVisible
    {
      get
      {
        return uiActionsControl.Visible;
      }
      set
      {
        uiActionsControl.Visible = value;
      }
    }

    private void foldersGrid_OpenSelected(object sender, EventArgs e)
    {
      OpenSelected();
    }
	}
}
